﻿import com.dmotiko.general.*
import com.dmotiko.util.Point;
import mx.data.types.Obj;
import mx.transitions.*
import mx.transitions.easing.*
import com.dmotiko.lib.ScrollerMovie
import mx.utils.Delegate;

class SelectorList
extends BaseClip {
	
	/* FLA VARS */
	private var mcBack:MovieClip;
	private var mcDragger:MovieClip;
	private var mcBar:MovieClip;
	private var mcMask:MovieClip;
	
	private var mcContent:MovieClip;
	private var aList:Array;
	private var nColorOver:Number;
	private var nColorNormal:Number;
	
	private function initClip() {
		createContent();
		mcBar._visible = false;
		mcDragger._visible = false;
		setSize( new Point( mcBack._width, mcBack._height ) );
		this.mcMask._alpha = 0;
		this._alpha = 0;
		this._visible = false;
		bShowed = false;
		nColorOver = 0x4444EE;
		nColorNormal = 0;
	}
		
	public function drawList(filter:String):Void {
		if (this._alpha < 100) show();
		aList = new Array();
		for( var i=0; i < data.length; i++){
			var str = data[i].key.substr( 0, filter.length ).toLowerCase();
			if( filter == str ) aList.push( data[i] );
		}
		createContent();
		showContent();
	}
		
	private function createContent():Void {
		var nDepth:Number;
		if (mcContent) nDepth = mcContent.getDepth(); 
		else nDepth = this.getNextHighestDepth();
		mcContent = this.createEmptyMovieClip("mcContent", nDepth);
		mcContent.setMask(mcMask);
	}
	
	/**
	 *  @author: sminutoli
	 *  @usage: basic view, override if you want another behaviour
	 */
	
	private function showContent():Void {
		
		if ( aList.length == 0) {
			
			hide();
			return;
			
		}
		
		var nY:Number = 0;
		var nDelay:Number = 0;
		var nDelayCount:Number = nDelay;
		var index:Number = 0;
		var clip;
		var selectorList:SelectorList = this;
		mcContent._x = 2;
		mcDragger._y = mcBar._y;
		this.onEnterFrame = function(){
			if(nDelayCount == 0){
				if( index == aList.length ){
					delete this.onEnterFrame;
					selectorList.dispatchEvent( { type: BaseClipEvent.READY, target: selectorList } );
					return;
				}
				nDelayCount = nDelay;
				clip = mcContent.attachMovie("SelectorListItem", "mcItem"+index, index);
				clip.label.text = aList[index].key;
				clip._alpha = 0;
				clip._x = 0;
				clip._y = 0;
				clip.initX = 0;
				clip.data = aList[index];
				clip.colorTween = new TweenRGB(clip, "", Regular.easeOut, selectorList.nColorOver, selectorList.nColorNormal, 1.5, true);
				clip.onRollOver = function(){
					if(this.tween) this.tween.stop();
					this.mcBack.gotoAndStop(2);
					this.tween = new Tween(this, "_x", Elastic.easeOut, this._x, this.initX+3, 1.5, true);
					this.colorTween = new TweenRGB(this, "", Regular.easeOut, selectorList.nColorNormal, selectorList.nColorOver, 1.5, true);
				}
				clip.onRollOut = function(){
					this.mcBack.gotoAndStop(1);
					if(this.tween) this.tween.stop();
					this.tween = new Tween(this, "_x", Elastic.easeOut, this._x, this.initX, 0.5, true);
					this.colorTween = new TweenRGB(this, "", Regular.easeOut, selectorList.nColorOver, selectorList.nColorNormal, 1.5, true);
				}
				clip.onRelease = function(){
					selectorList.dispatchEvent( { type: BaseClipEvent.CHANGED, target: selectorList, selectedItem: this.data } );
					//selectorList.hide();
				}
				
				/* back tween */
				if ( index == 0 ) {
					var endY:Number = clip._height * aList.length;
					if ( endY < mcMask._height ) {
						registerTween( "backTween", new Tween( mcBack, "_height", Regular.easeOut, mcBack._height, endY + 2 , 0.3, true) );
						ScrollerMovie.releaseScroll( mcContent, mcDragger, mcBar );
					} else {
						registerTween( "backTween", new Tween( mcBack, "_height", Regular.easeOut, mcBack._height, size.getY() , 0.3, true) );
						ScrollerMovie.setScroll( mcContent, mcMask, mcDragger, mcBar );
					}
				}
				
				this.registerTween( "selectItem"+index+"motion", new Tween( clip, "_y", Elastic.easeOut, clip._y, nY, 1, true) );
				this.registerTween( "selectItem"+index+"fade", new Tween( clip, "_alpha", Regular.easeOut, clip._alpha, 100, 0.3, true) );
				nY += clip._height;
				index ++;
					
			} else {
				nDelayCount --;
			}
		}
	}
	
	private function refreshData() {
		show();
	}
	
	private function refreshSize() {
		mcBack._width = size.getX();
		mcBack._height = size.getY();
		mcMask._width = size.getX() - mcBar._width;
		mcMask._height = mcBack._height;
		mcBar._height = mcBack._height;
		mcBar._x = mcMask._x + mcMask._width + 2;
		mcDragger._x = mcBar._x;
	}
	
	public function show() {
		this._visible = true;
		registerTween( "selectorFadeIn", new Tween( this, "_alpha", Regular.easeOut, this._alpha, 100, 0.5, true) );
		super.show();
	}
	public function hide() {
		var tween = new Tween( this, "_alpha", Regular.easeOut, this._alpha, 0, 0.5, true);
		registerTween( "selectorFadeOut",  tween, false, true);
		super.hide();
	}
	
	private function tweenFinished( key, tween ) {
		if ( key == "selectorFadeOut" ) this._visible = false;
	}
	
}